package cn.com.cfae.iras.doc.analyze.components.rationality;

import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

;

/**
 * 内容合理性
 * M-11
 * “发行人违反上述约定，投资人有权向人民法院提起诉讼或提交仲裁委员会仲裁”，
 * 1、提起诉讼或者提交仲裁委员会仲裁2选1
 * 2、诉讼、仲裁互斥
 * 3、待确认 （不同时存在正确） 同时存在就提示，
 *
 * 预评/发行阶段
 */
@Component("R1009")
@Scope("prototype")
public class R1009 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(R1009.class);
    @Autowired
    private HandingData handingData;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取R1009组件M");
        List<ComponentResult> componentResults = checkRulesR1009(getCatalogId(),getCatalogId(),StringUtils.isNotEmpty(getChapterId())?getChapterId():"M-11",getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        log.info("获取R1009组件BM");
        List<ComponentResult> componentResults = checkRulesR1009(getCatalogId(),getCatalogId(),StringUtils.isNotEmpty(getChapterId())?getChapterId():"M-11",getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取R1009组件DM");
        List<ComponentResult> componentResults = checkRulesR1009(getCatalogId(),getCatalogId(),StringUtils.isNotEmpty(getChapterId())?getChapterId():"DM-10",getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        log.info("获取R1009组件BDM");
        List<ComponentResult> componentResults = checkRulesR1009(getCatalogId(),getCatalogId(),StringUtils.isNotEmpty(getChapterId())?getChapterId():"BDM-10",getRuleTitle());
        return componentResults;
    }

    private List<ComponentResult> checkRulesR1009(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;
        boolean flag = false;
        int startPageNum = 0;
        String tqss = ".*提起诉讼.*";
        String tjzc = ".*仲裁委员会仲裁.*";
        Boolean tqssFlag = false;
        Boolean tjzcFlag = false;
        int tqPageNum = 0;
        List<ComponentResult.TextPosition> tqContentAndPositionList = null;
        int tjPageNum = 0;
        List<ComponentResult.TextPosition> tjContentAndPositionList = null;
        boolean isAllErr = false;
        boolean isAllSemm = false;
        try {

            if (extractData!=null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPageNum = chapterModel.getStartPage();
                tqPageNum = startPageNum;
                tjPageNum = startPageNum;
                if (chapterModel!=null){
                    List<PageModel> pageModelList = chapterModel.getPageModelList();
                    if (CollectionUtils.isNotEmpty(pageModelList)){
                        //提起诉讼是否存在
                        for (PageModel pageModel : pageModelList){
                            Map<String, Object> containsKeyWord = handingData.isContainsKeyWordOfRegex(pageModel.getJsonTextData(), tqss);
                            flag = (boolean)containsKeyWord.get("flag");
                            if (flag){
                                tqssFlag = true;
                                tqPageNum = pageModel.getPageNumber();
                                tqPageNum = tqPageNum!=0?tqPageNum:startPageNum;
                                tqContentAndPositionList =(List<ComponentResult.TextPosition>) containsKeyWord.get("position");
                                break;
                            }
                        }
                        //提交仲裁委员会仲裁是否存在
                        for (PageModel pageModel : pageModelList){
                            Map<String, Object> containsKeyWord = handingData.isContainsKeyWordOfRegex(pageModel.getJsonTextData(), tjzc);
                            flag = (boolean)containsKeyWord.get("flag");
                            if (flag){
                                tjzcFlag = true;
                                tjPageNum = pageModel.getPageNumber();
                                tjPageNum = tjPageNum!=0?tjPageNum:startPageNum;
                                tjContentAndPositionList =(List<ComponentResult.TextPosition>) containsKeyWord.get("position");
                                break;
                            }
                        }
                    }
                }
            }
            if (tqssFlag){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-发行人违反上述约定,投资人有权向人民法院提起诉讼","已出现",true,getRegulationId(),tqPageNum,tqContentAndPositionList,"02");
                componentResults.add(componentResult);
            }else {

                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-发行人违反上述约定,投资人有权向人民法院提起诉讼","未出现",false,getRegulationId(),tqPageNum,tqContentAndPositionList,"01");
                componentResults.add(componentResult);
            }
            if (tjzcFlag){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-提交仲裁委员会仲裁","已出现",true,getRegulationId(),tjPageNum,tjContentAndPositionList,"02");
                componentResults.add(componentResult);
            }else {

                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"关键词检查-提交仲裁委员会仲裁","未出现",false,getRegulationId(),tjPageNum,tjContentAndPositionList,"01");
                componentResults.add(componentResult);
            }

            if (tqssFlag&&tjzcFlag){
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"诉讼与仲裁裁措施检查","该文档内容披露不合理",true,getRegulationId(),tqPageNum,tqContentAndPositionList,"02");
                componentResults.add(componentResult);
            }else if (!tqssFlag&&!tjzcFlag){
                isAllSemm = true;
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"诉讼与仲裁裁措施检查","未出现,建议人工核对",true,getRegulationId(),tqPageNum,tqContentAndPositionList,"03");
                componentResults.add(componentResult);
            }else {
                isAllErr = true;
                componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"诉讼与仲裁裁措施检查","合理",false,getRegulationId(),tqPageNum,tqContentAndPositionList,"01");
                componentResults.add(componentResult);
            }

            if (isAllErr){
                for (ComponentResult result:componentResults){
                    result.setExist("01");
                }
            }
            if (isAllSemm){
                for (ComponentResult result:componentResults){
                    result.setExist("03");
                }
            }
        }catch (Exception e){
            componentResults.clear();
            componentResult =  handingData.getComponentResult(catalogId,chapterId,ruleName,"诉讼与仲裁裁措施检查","未识别,建议人工核对",true,getRegulationId(),startPageNum,contentAndPositionList,"03");
            componentResults.add(componentResult);
            log.error(e.getMessage(),e);
        }
        return componentResults;
    }
}
